from database import SessionLocal

from employment import crud
from fastapi import  Depends,  APIRouter
from sqlalchemy.orm import Session
from schema import Stu_score
import score.crud


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

score_api = APIRouter()

# 查询所有成绩
@score_api.get('/score/all',summary="查询所有的成绩")
async def get_all_score(db: Session = Depends(get_db)):
    return score.crud.get_stu_score_all(db)

# 根据学生姓名去查询
@score_api.get("/score/search_by_name",summary="根据学生姓名查询成绩")
async def get_score_by_name(stu_name:str,db: Session = Depends(get_db)):
    return score.crud.get_stu_score_by_name(db,stu_name)

# 添加学生成绩
@score_api.post("/score/add",summary="添加学生成绩")
def add_stu_score(stu_score:Stu_score,db: Session = Depends(get_db)):
    return score.crud.add_stu_score(stu_score=stu_score,db=db)

# 更新学生成绩
@score_api.post('/score/update',summary="更新学生成绩")
def update_score(stu_score:Stu_score,db: Session = Depends(get_db)):
    return score.crud.update_stu_score(stu_score=stu_score,db=db)

# 删除学生成绩
@score_api.get('/score/delete',summary="删除学生成绩")
def delete_score(stu_id:int,rnd:int,db: Session = Depends(get_db)):
    return score.crud.delete_stu_score(stu_id=stu_id,rnd=rnd,db=db)